package com.zzyl.service.impl;

import com.zzyl.entity.Bed;
import com.zzyl.enums.BasicEnum;
import com.zzyl.exception.BaseException;
import com.zzyl.mapper.BedMapper;
import com.zzyl.service.BedService;
import com.zzyl.vo.BedVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class BedServiceImpl implements BedService {

    @Autowired
    private BedMapper bedMapper;

    @Override
    public List<BedVo> getBedsByRoomId(Long roomId) {
        return bedMapper.getBedsByRoomId(roomId);
    }

    @Override
    public BedVo getBedById(Integer bedId) {
        Bed bed = bedMapper.getById(bedId);
        if (bed == null) {
            throw new BaseException(BasicEnum.BED_NOT_EXIST);
        }
        BedVo bedVo = new BedVo();
        BeanUtils.copyProperties(bed, bedVo);
        return bedVo;
    }

    @Override
    public void addBed(BedVo bedVo) {
        Bed bed = new Bed();
        BeanUtils.copyProperties(bedVo, bed);
        bed.setCreateTime(LocalDateTime.now());
        bed.setUpdateTime(LocalDateTime.now());
        bed.setBedStatus(0);
        try {
            bedMapper.insertBed(bed);
        }catch (Exception e){
            throw new BaseException(BasicEnum.BED_INSERT_FAIL);
        }
    }

    @Override
    public void updateBed(BedVo bedVo) {
        Bed bed = new Bed();
        BeanUtils.copyProperties(bedVo, bed);
        bed.setUpdateTime(LocalDateTime.now());
        try {
            bedMapper.updateBed(bed);
        }catch (Exception e){
            throw new BaseException(BasicEnum.BED_UPDATE_FAIL);
        }
    }

    @Override
    public void deleteBed(Integer bedId) {
        Bed bed = bedMapper.getById(bedId);
        if (bed == null) {
            throw new BaseException(BasicEnum.BED_NOT_EXIST);
        }
        try {
            bedMapper.deleteById(bedId);
        } catch (Exception e) {
            throw new BaseException(BasicEnum.BED_DELETE_FAIL);
        }
    }
}

